package com.cangxuan.manage.controller;

import com.cangxuan.manage.annotation.Check;
import com.cangxuan.manage.bean.Result;
import com.cangxuan.manage.entity.Banner;
import com.cangxuan.manage.service.IBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 轮播相关控制器
 * Author:罗健
 */
@Api(tags = "轮播相关接口")
@RestController
@RequestMapping("banner")

public class BannerController extends BaseController{
    IBannerService bannerService;
    @Autowired
    public void setBannerService(IBannerService bannerService) {
        this.bannerService = bannerService;
    }
    /**
     * 获取轮播
     * @param
     * @return
     */
    @GetMapping("get")
    @ApiOperation("查询轮播")
    public Object get(){
        return success("请求成功", bannerService.getBanner());
    }
    /**
     * 增加轮播
     * @param banner
     * @return
     */
    @PostMapping("save")
    @ApiOperation("增加轮播")
    public Object save(@Check @RequestBody Banner banner) throws IOException {
        boolean save = bannerService.saveBanner(banner);
        return success(save);
    }
    /**
     * 修改
     * @param banner
     * @return
     */
    @PostMapping("up")
    @ApiOperation("修改轮播")
    public Object up(@Check @RequestBody Banner banner) {
        boolean save = bannerService.upDataBanner(banner);
        return success(save);
    }
    /**
     * 根据id删除轮播
     * @param id
     * @return
     */
    @GetMapping("remove")
    @ApiOperation("删除轮播")
    public Object remove(@RequestParam int id) {
       bannerService.removeBanner(id);
        return success();
    }
    @Value("${webPath}")
    String webPath;

    /**
     * banner文件上传
     * @param partFile
     * @return
     */
    @PostMapping("bannerUploadImage")
    @ApiOperation("上传banner图片")
    public Object uploadImage(@ModelAttribute MultipartFile partFile) throws IOException {
        //文件web路径
        String filePath = "/upload/banner/" + System.currentTimeMillis() + ".png";
        //设置文件保存的磁盘路径
        String uploadPath = webPath + filePath;
        //实例一下文件
        File imageFile = new File(uploadPath);
        //将接收到的上传对象写入到imageFile对象中
        partFile.transferTo(imageFile);
        return success("上传成功", filePath);
    }
}
